<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 登录账号设置 -->
            <el-form
              ref="form"
              label-width="120px"
              :model="form"
              :rules="rules"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item prop="username" label="姓名:">
                <el-input v-model="form.username" style="width: 300px" />
              </el-form-item>
              <el-form-item prop="password2" label="密码:">
                <el-input
                  v-model="form.password2"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 打印按钮 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 个人详情组件 -->
            <userInfo></userInfo>
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 打印按钮 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 岗位信息组件 -->
            <jobInfo></jobInfo>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getProfileById } from '@/api/user'
import { updateUserProfile } from '@/api/employees'
import userInfo from './components/user-info.vue'
import jobInfo from './components/job-info.vue'
export default {
  data() {
    return {
      form: {
        username: '',
        password2: ''
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password2: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码需要6-16位', trigger: 'blur' }
        ]
      },
      userId: this.$route.params.id
    }
  },
  components: {
    userInfo,
    jobInfo
  },
  created() {
    // 获取员工基本信息
    this.getUserInfo()
  },
  methods: {
    // 获取用户基本信息
    async getUserInfo() {
      this.form = await getProfileById(this.userId)
    },
    // 更新用户基本信息
    async updateUser() {
      await this.$refs.form.validate()
      await updateUserProfile({ ...this.form, password: this.form.password2 })
      this.$message.success('更新成功')
    }
  }
}
</script>

<style>
</style>